<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BWInputMedia
 *
 * @author jianfeng
 */
class BWInputMedia extends BWEntityWithGroup {

    const Input_Type_KeyValue = 'key_value';

    public $name;
    public $type;
    public $content;
    public $activeTime;
    public $expireTime;
    public $inputLimit;
    public $createTime;
    public $owner;
    public $ownerType;
    public $ownerId;
    protected $isLoaded = array();
    public $inputableList = array();
    public $inputTypeClass = null;
    protected $inputType = null;
    protected $messageManager = null;
    protected $mediaManager = null;
    public $inputValueList = array();

    public function __construct($item = NULL, $context = NULL) {

        if ($item instanceof BEntity && $item->getId() != NULL) {
            $this->owner = $item;
            $this->ownerId = $item->getId();
            $this->ownerType = get_class($item);
        }

        parent::__construct($item, $context);
    }

    public function getContent($data = NULL) {
        return $this->content;
    }

    public function save($context = null) {
        if ($this->inputType instanceof BWInputType)
            $this->inputTypeClass = get_class($this->inputType);
        parent::save($context);
    }

    //put your code here
    public function getInputValue($inputValueContext = NULL, $returnLastValue = TRUE) {
        //if (empty($this->inputValueList)) {
        $this->loadInputValueList($inputValueContext);
        //}
        $valueList = $this->inputValueList;

        //if not set context, return first inputValue
        if (is_array($valueList) && $valueList != NULL) {
            if (!$returnLastValue)
                return reset($valueList);
            else {
                return end($valueList);
            }
        }
    }

    public function getInputValueCount($inputValueContext = NULL, $data = NULL) {
        $inputValue = new BWInputValue();
        $findContext = array(
            'spaceId' => Blyn::app()->getActiveSpace()->getId(),
            'appId' => Blyn::app()->getActiveApp()->getId(),
            'inputMediaId' => $this->getId()
        );
        if (is_array($inputValueContext)) {
            $findContext += $inputValueContext;
        }
        $count = $inputValue->getRepository()->getCount($findContext);
        return $count;
    }

    public function getInputValueList($inputValueContext = array(), $data = NULL) {
        if (empty($this->inputValueList)) {
            $this->loadInputValueList(NULL, $inputValueContext);
        }
        $valueList = $this->inputValueList;
        foreach ($valueList as $inputValue) {
            if ($inputValue instanceof BWInputValue) {
                if ($inputValue->type == 'none-string')
                    $inputValue->content = unserialize($inputValue->content);
            }
        }
        if ($inputValueContext == NULL)
            return $valueList;
        else {
            $list = array();

            foreach ($valueList as $id => $inputValue) {
                if (is_string($inputValueContext)) {
                    $name = $inputValueContext;
                    if ($name == $inputValue->getName())
                        $list[$id] = $inputValue;
                }
                if (is_array($inputValueContext)) {
                    if ($inputValue instanceof BEntity) {
                        $propertyList = $inputValue->getPropertyList();
                        $isSelected = TRUE;
                        foreach ($propertyList as $name => $value) {
                            if (isset($inputValueContext[$name]) && $inputValueContext[$name] != $value)
                                $isSelected = FALSE;
                        }
                        if ($isSelected)
                            $list[$id] = $inputValue;
                    }
                }
                if ($inputValueContext instanceof BWInputValue) {
                    $cProperList = $inputValueContext->getPropertyList();
                    $mProperList = $inputValue->getPropertyList();
                    $isSelected = TRUE;
                    foreach ($mProperList as $name => $value) {
                        if (isset($cProperList[$name]) && $cProperList[$name] != $value)
                            $isSelected = FALSE;
                    }
                    if ($isSelected)
                        $list[$id] = $inputValue;
                }
            }
            return $list;
        }
    }

    public function addInputValue($inputValueData = NULL, $data = NULL) {
        $inputValue = NULL;

        if ($inputValueData instanceof BWInputValue) {
            $inputValue = $inputValueData;
            //$inputValue->save();
        } else {
            $inputValue = new BWInputValue();

            if (is_string($inputValueData) || is_numeric($inputValueData)) {
                $inputValue->type = 'string';
                $inputValue->content = $inputValueData;
            } else {
                $inputValue->type = 'none-string';
                $inputValue->data = serialize($inputValueData);
            }

            //$inputValue->save();
        }

        if ($inputValue instanceof BWInputValue) {
            //$inputType = $this->getInputType();        
            $inputValue->inputMediaId = $this->getId();
            $inputValue->appId = Blyn::app()->getActiveApp()->getId();
            $inputValue->spaceId = Blyn::app()->getActiveSpace()->getId();
            $inputValue->creatorId = Blyn::app()->getCurrentUser()->getId();
            $inputValue->inputTime = time();
            $inputValue->modifyTime = time();
            $inputValue->save();
        }
        //reload list for keeping update for valuelist
        $this->loadInputValueList(NULL, TRUE);
    }

    public function removeInputValue($inputValue, $data = NULL) {
        if ($inputValue instanceof BWInputValue) {
            if ($inputValue->getId() != NULL) {
                $inputValue->inputMediaId = NULL;
                $inputValue->save();
            }
        }
        $this->loadInputValueList(NULL, TRUE);
    }

    public function loadInputValueList($data = NULL, $reload = FALSE) {
        if ($reload == NULL)
            $reload = FALSE;
        if ($data == NULL) {
            $data = 'defaultFindContext';
        }
        if ($data != NULL) {
            if (is_array($data))
                $aData = json_encode($data);
            if (is_string($data))
                $aData = $data;
            if (is_string($aData) && $aData != NULL)
                $findContextId = md5($aData);
        }
        if ($reload || !isset($this->isLoaded[$findContextId]) || (isset($this->isLoaded[$findContextId]) && $this->isLoaded[$findContextId] != TRUE)) {
            $inputValue = new BWInputValue();
            $findContext = array(
                'spaceId' => Blyn::app()->getActiveSpace()->getId(),
                'appId' => Blyn::app()->getActiveApp()->getId(),
                'inputMediaId' => $this->getId());
            if (is_array($data)) {
                $findContext += $data;
            }
            $view = $inputValue->getRepository()->getView($findContext);
            $rows = $view->rows;

            $list = array();

            foreach ($rows as $row) {
                $inputValue = new BWInputValue();
                $inputValue->load($row);
                $list[$inputValue->getId()] = $inputValue;
            }

            $this->inputValueList = $list;
            $this->isLoaded[$findContextId] = TRUE;
        }
    }

    public function isAvailable() {
        
    }

    public function getRepository($data = NULL, $reload = FALSE) {
        $repository = parent::getRepository($data, $reload);
        $repository->addPropertyToColumnMap('content', 'input_content');
        $repository->addPropertyToColumnMap('activeTime', 'active_time');
        $repository->addPropertyToColumnMap('expireTime', 'expire_time');
        $repository->addPropertyToColumnMap('createTime', 'create_time');
        $repository->addPropertyToColumnMap('inputLimit', 'input_limit');
        $repository->addPropertyToColumnMap('inputTypeClass', 'input_type');
        $repository->addPropertyToColumnMap('ownerType', 'resource_class');
        $repository->addPropertyToColumnMap('ownerId', 'resource_id');

        return $repository;
    }

    public function addAttribute($name, $value, $data = array()) {
        $data['attributeClass'] = 'BWInputAttribute';
        return parent::addAttribute($name, $value, $data);
    }

    public function getAttribute($name, $data = array()) {
        $data['attributeClass'] = 'BWInputAttribute';
        return parent::getAttribute($name, $data);
    }

    public function getAttributes($data = array()) {
        $data['attributeClass'] = 'BWInputAttribute';
        return parent::getAttributes($data);
    }

    public function removeAttribute($name, $data = array()) {
        $data['attributeClass'] = 'BWInputAttribute';
        return parent::removeAttribute($name, $data);
    }

    public function getInputType($data = NULL) {

        if ($this->inputType == NULL) {
            $inputTypeClass = $this->inputTypeClass;
            $this->inputType = new $inputTypeClass($this);
        }
        return $this->inputType;
    }

    public function setInputType($inputType, $data = NULL) {
        if ($inputType instanceof BWInputType) {
            $this->inputType = $inputType;
            $this->inputTypeClass = get_class($inputType);
            $this->inputType->setInputMedia($this);
            //save inputType into database
            //$this->save();
        }
    }

    public function getMessageManager($data = NULL) {
        if ($this->messageManager == NULL)
            $this->messageManager = new BWMessageManager($this);

        return $this->messageManager;
    }

    public function getMediaManager($data = NULL) {
        if ($this->mediaManager == NULL)
            $this->mediaManager = new BWMediaManager($this);

        return $this->mediaManager;
    }

}
